package com.example.scenictourback.service.impl;

import com.example.scenictourback.dto.ScenicAreaDTO;
import com.example.scenictourback.dto.ScenicSpotDTO;
import com.example.scenictourback.entity.ScenicArea;
import com.example.scenictourback.entity.ScenicSpot;
import com.example.scenictourback.repository.ScenicAreaRepository;
import com.example.scenictourback.repository.ScenicSpotRepository;
import com.example.scenictourback.service.ScenicAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ScenicAreaServiceImpl implements ScenicAreaService {

    @Autowired
    private ScenicAreaRepository scenicAreaRepository;
    
    @Autowired
    private ScenicSpotRepository scenicSpotRepository;

    @Override
    public List<ScenicAreaDTO> listAllScenicAreas() {
        List<ScenicArea> scenicAreas = scenicAreaRepository.findAll();
        return ScenicAreaDTO.fromEntities(scenicAreas);
    }

    @Override
    public List<ScenicAreaDTO> listOpenScenicAreas() {
        List<ScenicArea> openScenicAreas = scenicAreaRepository.findByStatus(1);
        return ScenicAreaDTO.fromEntities(openScenicAreas);
    }

    @Override
    public ScenicAreaDTO getScenicAreaById(Integer id) {
        ScenicArea scenicArea = scenicAreaRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("景区不存在"));
        
        ScenicAreaDTO dto = ScenicAreaDTO.fromEntity(scenicArea);
        
        // 查询该景区下的所有景点
        List<ScenicSpot> spots = scenicSpotRepository.findByScenicAreaId(id);
        dto.setSpots(ScenicSpotDTO.fromEntities(spots));
        
        return dto;
    }

    @Override
    public List<ScenicAreaDTO> searchScenicAreasByName(String name) {
        List<ScenicArea> scenicAreas = scenicAreaRepository.findByNameContaining(name);
        return ScenicAreaDTO.fromEntities(scenicAreas);
    }

    @Override
    @Transactional
    public ScenicAreaDTO addScenicArea(ScenicArea scenicArea) {
        // 保存景区信息
        ScenicArea savedScenicArea = scenicAreaRepository.save(scenicArea);
        return ScenicAreaDTO.fromEntity(savedScenicArea);
    }

    @Override
    @Transactional
    public ScenicAreaDTO updateScenicArea(Integer id, ScenicArea scenicArea) {
        // 检查景区是否存在
        if (!scenicAreaRepository.existsById(id)) {
            throw new RuntimeException("景区不存在");
        }
        
        // 设置ID
        scenicArea.setId(id);
        
        // 更新景区信息
        ScenicArea updatedScenicArea = scenicAreaRepository.save(scenicArea);
        return ScenicAreaDTO.fromEntity(updatedScenicArea);
    }

    @Override
    @Transactional
    public void deleteScenicArea(Integer id) {
        // 检查景区是否存在
        if (!scenicAreaRepository.existsById(id)) {
            throw new RuntimeException("景区不存在");
        }
        
        // 删除景区
        scenicAreaRepository.deleteById(id);
    }
    
    @Override
    public Long countScenicAreas() {
        return scenicAreaRepository.count();
    }
    
    @Override
    public Long countOpenScenicAreas() {
        return scenicAreaRepository.countByStatus(1);
    }
} 